﻿using LD.PLC;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LD.Host.PLC
{
    internal class PLCEcho
    {
        public static OpResult Write(EchoModel model, bool isSuccess)
        {
            bool IsSuccess = false;
            string ResultMsg = string.Empty;
            try
            {
                if (model != null)
                {
                    if (model.Echo)
                    {
                        ushort short_v = 2;
                        int int_v = 2;
                        bool bool_v = false;
                        if (isSuccess)
                        {
                            short_v = 1;
                            int_v = 1;
                            bool_v = true;
                        }

                        switch (model.PlcType)
                        {
                            case "Int16":
                                if (model.ErrWritePLC && isSuccess)
                                {
                                    Console.WriteLine("失败写入PLC没有勾选并且成功不写PLC");
                                    IsSuccess = true;
                                }
                                else
                                {
                                    var op16 = PLCFactory.Instance.Write(model.PlcAddress, short_v);
                                    if (op16.IsSuccess)
                                        IsSuccess = true;
                                    ResultMsg = op16.Message;
                                }
                                break;
                            case "Int32":
                                if (model.ErrWritePLC && isSuccess)
                                {
                                    Console.WriteLine("失败写入PLC没有勾选并且成功不写PLC");
                                    IsSuccess = true;
                                }
                                else
                                {
                                    var op32 = PLCFactory.Instance.Write(model.PlcAddress, int_v);
                                    if (op32.IsSuccess)
                                        IsSuccess = true;
                                    ResultMsg = op32.Message;
                                }
                                break;
                            case "bool":
                                if (model.ErrWritePLC && isSuccess)
                                {
                                    Console.WriteLine("失败写入PLC没有勾选并且成功不写PLC");
                                    IsSuccess = true;
                                }
                                else
                                {
                                    var opbool = PLCFactory.Instance.Write(model.PlcAddress, bool_v);
                                    if (opbool.IsSuccess)
                                        IsSuccess = true;
                                    ResultMsg = opbool.Message;
                                }
                                break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ResultMsg = ex.Message;
                Log4.Error(ex.Message);
            }
            return new OpResult(IsSuccess, ResultMsg);
        }
        public static OpResult Read(string dataType, string address, int length)
        {
            OpResult opResult = new OpResult() { IsSuccess = false };

            if (string.IsNullOrEmpty(dataType) || string.IsNullOrEmpty(address))
            {
                return opResult;
            }

            try
            {
                switch (dataType)
                {
                    case "Int16":
                        var op16 = PLCFactory.Instance.ReadInt16(address);
                        if (op16.IsSuccess)
                            opResult = new OpResult() { IsSuccess = true, Append = op16.Content };
                        else
                            Log4.Error($"PLC读取:{address} ," + op16.Message);
                        break;
                    case "Int32":
                        var op32 = PLCFactory.Instance.ReadInt(address);
                        if (op32.IsSuccess)
                            opResult = new OpResult() { IsSuccess = true, Append = op32.Content };
                        else
                            Log4.Error($"PLC读取:{address} ," + op32.Message);
                        break;
                    case "Float":
                        var opfloat = PLCFactory.Instance.ReadFloat(address);
                        if (opfloat.IsSuccess)
                            opResult = new OpResult() { IsSuccess = true, Append = opfloat.Content };
                        else
                            Log4.Error($"PLC读取:{address} ," + opfloat.Message);
                        break;
                    case "Bool":
                        var opbool = PLCFactory.Instance.ReadBool(address);
                        if (opbool.IsSuccess)
                            opResult = new OpResult() { IsSuccess = true, Append = opbool.Content };
                        else
                            Log4.Error($"PLC读取:{address} ," + opbool.Message);
                        break;
                    case "String":
                        var opstring = PLCFactory.Instance.ReadString(address, (ushort)length);
                        if (opstring.IsSuccess)
                            opResult = new OpResult() { IsSuccess = true, Append = opstring.Content };
                        else
                            Log4.Error($"PLC读取:{address} ," + opstring.Message);
                        break;
                    case "Bool[]":
                        var opBools = PLCFactory.Instance.ReadBool(address, (ushort)length);
                        if (opBools.IsSuccess)
                            opResult = new OpResult() { IsSuccess = true, Append = opBools.Content };
                        else
                            Log4.Error($"PLC读取:{address}," + opBools.Message);
                        break;
                    case "Byte[]":
                        var opBytes = PLCFactory.Instance.Read(address, (ushort)length);
                        if (opBytes.IsSuccess)
                        {
                            opResult = new OpResult() { IsSuccess = true, Append = opBytes.Content };
                        }
                        else
                            Log4.Error($"PLC读取:{address}," + opBytes.Message);
                        break;
                    case "Bin2[]":
                        var opBin2 = PLCFactory.Instance.Read(address, (ushort)length);
                        if (opBin2.IsSuccess)
                        {
                            StringBuilder sb = new StringBuilder();
                            //数据解析为二进制  

                            for (int i = 0; i < opBin2.Content.Length; i++)
                            {
                                string strTemp = System.Convert.ToString(opBin2.Content[i], 2);
                                strTemp = strTemp.Insert(0, new string('0', 8 - strTemp.Length));
                                sb.Append(strTemp);
                            }
                            Log4.Info($"PLCByte读取:{address}," + sb.ToString());

                            //foreach (var item in opBin2.Content)
                            //{
                            //    string bin2Str = Convert.ToString(item);
                            //    sb.Append(bin2Str.Insert(0, new string('0', 8 - bin2Str.Length)));
                            //}
                            opResult = new OpResult() { IsSuccess = true, Append = sb.ToString().ToCharArray() };
                        }
                        else
                            Log4.Error($"PLC读取:{address}," + opBin2.Message);
                        break;
                }

            }
            catch (Exception ex)
            {
                Log4.Error($"PLC读取:{address},", ex);
            }

            Log4.Info($"PLC读取:{address}" + JsonConvert.SerializeObject(opResult));
            return opResult;
        }
    }
}
